Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Functions → position arguments

Python Functions

position arguments

In Python, functions are fundamental building blocks that encapsulate reusable code. When defining a function, you can specify parameters, which act as placeholders for values that will be passed to the function during its execution. Positional arguments are the simplest way to pass values to these parameters. Their significance lies in the fact that the order in which you provide them *directly* corresponds to the order of the parameters in the function definition.

Basic Concept

The function definition dictates the order and number of expected positional arguments. When calling the function, you supply the values in the same order. Python matches the supplied values to the parameters based on their positions.

Example 1: A simple function

positional arguments example def greet(name, greeting): """Greets the person with a specified greeting.""" print(f"{greeting}, {name}!") greet("Alice", "Hello") greet("Bob", "Good morning")

Output

Hello, Alice! Good morning, Bob!
Here, `name` and `greeting` are positional parameters. "Alice" is assigned to `name` because it's the first argument, and "Hello" is assigned to `greeting` because it's the second.

Example 2: Multiple positional arguments

Functions can take any number of positional arguments.
Multiple positional arguments example in python def add_numbers(x, y, z): """Adds three numbers together.""" return x + y + z result = add_numbers(5, 10, 15) print(result)

Output

30
The order here is crucial: `5` goes to `x`, `10` to `y`, and `15` to `z`.

Example 3: Default values and positional arguments

While positional arguments are primarily about order, Python allows combining them with parameters having default values. However, positional arguments must come *before* parameters with default values when calling the function.
Default values and positional arguments def describe_pet(animal_type, pet_name, age=3): # age has a default value """Describes a pet.""" print(f"I have a {animal_type}.") print(f"My {animal_type}'s name is {pet_name}.") print(f"It is {age} years old.") describe_pet("dog", "Buddy") # age defaults to 3 describe_pet("cat", "Whiskers", 5) # age is explicitly provided, overriding the default

Output

I have a dog. My dog's name is Buddy. It is 3 years old. I have a cat. My cat's name is Whiskers. It is 5 years old.

In the first call, `age` takes its default value. In the second, the provided `5` overrides the default. You cannot provide `age` before `animal_type` or `pet_name` because `age` is not a positional-only parameter.

Example 4: Using *args for a variable number of positional arguments

Python allows functions to accept a variable number of positional arguments using the `*args` syntax. `args` becomes a tuple containing all the extra positional arguments supplied during the call.
Using *args for a variable number of positional arguments def sum_all(*args): """Sums all numbers passed as arguments.""" total = 0 for num in args: total += num return total result = sum_all(1, 2, 3, 4, 5) print(result) result = sum_all(10, 20) print(result)

Output

15 30

`*args` provides flexibility to handle a varying number of inputs without explicitly defining all parameters upfront. However, remember that these arguments are still positional; their order matters within the `args` tuple. In summary, understanding positional arguments is crucial for writing clear, predictable, and correct Python functions. Mastering this fundamental concept lays a solid foundation for using more advanced function parameter techniques.

Tutorials